clc
t=0:pi/20:4*pi;
m=moviein(81);
clf
for i=1:80
x1=i*pi/10:pi/40:4*pi+i*pi/10;
x2=8*pi-i*pi/10:-pi/40:-i*pi/10+4*pi;
y1 = sin(x1-t(i)+pi/4);
y2 = sin(x2-t(i));
subplot(2,2,1)
plot(x1,y1);
axis([0 8*pi -1 1])
subplot(2,2,2)
plot(x2, y2);
axis([0 8*pi -1 1])
subplot(2,2,3)
x=intersect(x1,x2);
A=max(x);
B=min(x);
xx=B:pi/20:A;
y12 = sin(xx-t(i)+pi/4);
y22 = sin(xx-t(i));
y= y12+y22; 
plot(xx,y,x1,y1,x2,y2);
axis([0 8*pi -2 2]);
m(:,i)=getframe;
%pause(1)
end
subplot(2,2,4)
axis([0 4*pi -2 2]);
movie(m,3,30)